package com.person.jc.web.car;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.car.BdCarReturnService;
import com.person.jc.service.car.BdCarUseService;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.Utils;

public class BdCarReturnController extends Controller {
	
	public void init(){
		String para = getPara("menuId");
		if(para==null) {
			para = "06.01.02";
		}
		setAttr("menuId",para);
		render("main.jsp");
	}
	
	public void list(){
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
		map.put("operUser", tsUser.getUserId());
		List<Record> list = BdCarReturnService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void edit(){
		setAttr("now", DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		String logId = getPara("logId");
		String view = getPara("view");
		if(Utils.isNotEmpty(logId)){
			Map<String,Object> map = new HashMap<String, Object>();
			TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
			map.put("operUser", tsUser.getUserId());
			map.put("logId", logId);
			List<Record> list = BdCarUseService.me.getList(map);
			if(list.size() != 0){
				setAttr("obj", list.get(0));
			}
			setAttr("view", view);
		}
		render("edit.jsp");
	}
	
	public void save(){
		String res = "success";
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		try {
			TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
			map.put("operUser", tsUser.getUserId());
			BdCarReturnService.me.update(map);
		} catch (Exception e) {
			e.printStackTrace();
			res = "error";
		}
		renderText(res);
	}
	
	public void returnCarlist(){
		Map<String,Object> map = new HashMap<>();
		TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
		map.put("operUser", tsUser.getUserId());
		List<Record> list = BdCarReturnService.me.getList(map);
		setAttr("list", list);
		render("returnCarList.jsp");
	}
	
}
